<?php
namespace Mouta\AdminBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;

use Mouta\AmistateBundle\Entity\Usuario;

class ImageAdmin extends Admin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
		//$image = $this->subject;
		
        $formMapper
            ->add('nombre')
            ->add('principal','checkbox',array('attr' => array('chk_prin' => true),'required' => false))
            ->add('path','image_preview', array('image_path' => 'webPath','required' => false))
            ->add('file','file',array('required' => false))
            ->add('usuario','sonata_type_model_hidden')
        ;
    }

    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
            ->add('nombre')
        ;
    }

    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->addIdentifier('id')
            ->add('nombre')
        ;
    }
	
	public function getNewInstance()
    {
        $image = parent::getNewInstance();
		$usuarioId = $this->getRequest()->get('objectId');
		$usuario = $this->getModelManager()->find('Mouta\AmistateBundle\Entity\Usuario', $usuarioId);
        if ($this->hasRequest()) {
            $image->setUsuario($usuario);
        }
        return $image;
    }
}